<template>
  <div id="login">
    <p class="title">投资理财管理系统</p>
    <div class="login-wrap">
      <!--表单开始-->
      <el-form
        :model="loginForm"
        :rules="rules"
        ref="refForm"
        label-width="60px"
        class="demo-ruleForm"
      >
        <el-form-item label="姓  名" prop="user">
          <el-input type="text" v-model="loginForm.user"></el-input>
        </el-form-item>
        <el-form-item label="密  码" prop="password">
          <el-input type="password" v-model="loginForm.password"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="reset">重置</el-button>
          <el-button type="primary" @click="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import sha1 from "js-sha1";
import { reactive, ref, getCurrentInstance, unref } from "vue";

export default {
  name: "login",
  setup() {
    const refForm = ref();
    const loginForm = reactive({
      user: "wangps",
      password: ""
    });
    const rules = {
      user: [
        { required: true, message: "请输入账户", trigger: "blur" },
        { min: 3, max: 10, message: "长度在3到10字符", trigger: "blur" }
      ],
      password: [
        { required: true, message: "请输入密码", trigger: "blur" },
        { min: 3, max: 10, message: "长度在3到10字符", trigger: "blur" }
      ]
    };
    const { proxy } = getCurrentInstance();
    const reset = () => {
      const form = unref(refForm);
      form.resetFields();
    };
    const submit = () => {
      let requestData = {
        user: loginForm.user,
        password: sha1(loginForm.password)
      };
      proxy.$store
        .dispatch("app/login", requestData)
        .then(() => {
          proxy.$router.push({ name: "Console" });
        })
        .catch(error => {
          proxy.$message.error(error);
        });
    };
    return {
      loginForm,
      rules,
      reset,
      submit,
      refForm
    };
  }
};
</script>

<style lang="scss" scoped>
#login {
  height: 100vh;
  background-image: linear-gradient(
    to bottom,
    rgba(50, 50, 50, 1),
    rgba(100, 100, 100, 0.8)
  );
  text-align: center;
}
.title {
  color: white;
  font-family: 华文行楷;
  font-size: xxx-large;
  padding-top: 160px;
}
.login-wrap {
  width: 400px;
  margin: 60px auto;
  padding: 40px;
  background-color: rgba(255, 255, 255, 0.8);
  border-radius: 20px;
}
</style>
